﻿using B.S.XZYData.Api.Read.Application.Command.Permission;
using B.S.XZYData.Domain.RBAC;
using B.S.XZYData.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace B.S.XZYData.Api.Read.Controllers
{
    /// <summary>
    /// 权限控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PermissionController : ControllerBase
    {
        private readonly IMediator mediator;

        public PermissionController(IMediator mediator)
        {
            this.mediator = mediator;
        }
        /// <summary>
        /// 查询权限列表方法
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回查询到的权限列表</returns>
        [HttpGet]
        [Authorize]
        public Task<ApiPageing<PermissionModel>> GetPermission([FromQuery]GetPermissionCommand reques)
        {
            return mediator.Send(reques);
        }
        /// <summary>
        /// 查询所有权限信息
        /// </summary>
        /// <returns>所有权限列表</returns>
        [HttpGet]
        [Authorize]
        public async Task<ApiResult<List<PermissionModel>>> GetAllPermission([FromQuery]GetAllPermissionCommand request)
        {
            var result = await mediator.Send(request);
            return result;
        }
        /// <summary>
        /// 通过角色ID列表查询权限列表
        /// </summary>
        /// <param name="request">包含角色ID列表的命令</param>
        /// <returns>权限列表</returns>
        [HttpPost]
        [Authorize]
        public async Task<ApiResult<List<PermissionModel>>> GetPermissionsByRoleIds(GetPermissionsByRoleIdsCommand request)
        {
            var result = await mediator.Send(request);
            return result;
        }
        
        /// <summary>
        /// 根据ID获取权限详情
        /// </summary>
        /// <param name="id">权限ID</param>
        /// <returns>权限详情</returns>
        [HttpGet]
        [Authorize]
        public async Task<ApiResult<PermissionModel>> GetPermissionById(long id)
        {
            var command = new GetPermissionByIdCommand { Id = id };
            var result = await mediator.Send(command);
            return result;
        }
    }
}
